package org.twnmayberry.ejb;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.twnmayberry.dto.POIDTO;

@Stateless
public class MayberryTimerEJB {

	private static Log log = LogFactory.getLog(MayberryTimerEJB.class);
	
	@EJB
	private PoiMgmtRemote poiManager;

	@PostConstruct
	public void init() {
	}

	
	@Schedule(second = "*/30", minute = "*", hour = "*", dayOfMonth = "*", month = "*", year = "*",persistent=false)
	public void publishAllPOIs(Timer timer) {
      log.info("Scheduled publish of all POIs");
      List<POIDTO> pois = poiManager.getAllPOI();
      for(POIDTO poi : pois) {
    	  boolean danger = poiManager.isDangerous(poi);
    	  poiManager.publishPOIChange(poi, danger);
      }
	}
}
